﻿using System.Collections.Generic;
using System.Linq;
using ELearningSystem.DAO.DAO;
using ELearningSystem.Gateway.Entities;

namespace ELearningSystem.Core.Manager.ExtensionMethod
{
    public static class Extensions
    {
        public static TreeContentDAO ToTreeContentDAO(this TreeContent treeContent)
        {
            if (treeContent==null)
            {
                return new TreeContentDAO();
            }
            var treeContentDao = new TreeContentDAO
                                                {
                                                    Id = treeContent.Id,
                                                    Text = treeContent.Text,
                                                    Content = treeContent.Content.ToContentDAO()
                                                };
            if (treeContent.ParentNode==null)
            {
                treeContentDao.ParentNode = null;
            }
            else
            {
                treeContentDao.ParentNode = treeContent.ParentNode.ToTreeContentDAO();
            }
            return treeContentDao;
        }
        public static TreeContent ToTreeConentEntity(this TreeContentDAO treeContentDAO)
        {
            if (treeContentDAO ==null)
            {
                return new TreeContent();
            }
            TreeContent treeContent = new TreeContent
                                          {
                                              Id = treeContentDAO.Id,
                                              Text = treeContentDAO.Text,
                                              Content = treeContentDAO.Content.ToContentEntity(),
                                              Position = treeContentDAO.Position
                                          };
            if (treeContentDAO.ParentNode == null)
            {
                treeContent.ParentNode = null;
            }
            else
            {
                treeContent.ParentNode = treeContentDAO.ParentNode.ToTreeConentEntity();
            }
            return treeContent;
        }
        public static Content ToContentEntity(this ContentDAO contentDAO)
         {
            if (contentDAO==null)
            {
                return new Content();
            }
            var content = new Content
                                  {
                                      Id = contentDAO.Id,
                                      IsVideo = contentDAO.IsVideo,
                                      Video = contentDAO.Video.ToVideoEntity(),
                                      Practice = contentDAO.Practice.ToPracticeEntity()
                                  };
            return content;
         }

        public static ContentDAO ToContentDAO (this Content content)
        {
            if (content==null)
            {
                return new ContentDAO();
            }
           ContentDAO contentDAO = new ContentDAO(){
                Id = content.Id,
                IsVideo = content.IsVideo,
                Video = content.Video.ToVideoDAO(),
                Practice = content.Practice.ToPracticeDAO()
                
            };
            return contentDAO;
        }
        public static VideoContent ToVideoEntity(this VideoContentDAO videoContentDAO)
        {
            if (videoContentDAO==null)
            {
                return new VideoContent();
            }
            var videoContent = new VideoContent
                                   {
                                       Id = videoContentDAO.Id,
                                       Link = videoContentDAO.Link,
                                       Text = videoContentDAO.Text
                                   };
            foreach (var comment in videoContentDAO.Comments)
            {
                videoContent.Comments.Add(comment.ToEntity());
            }
            foreach (var material in videoContentDAO.Materials)
            {
                videoContent.Materials.Add(material.ToEntity());
            }
            foreach (var description in videoContentDAO.Descriptions)
            {
                videoContent.Descriptions.Add(description.ToEntity());
            }
            foreach (var referenceDAO in videoContentDAO.References)
            {
                videoContent.References.Add(referenceDAO.ToEntity());
            }
            return videoContent;
        }
        public static VideoContentDAO ToVideoDAO(this VideoContent videoContent)
        {
            if (videoContent==null)
            {
                return new VideoContentDAO();
            }

            var list = new List<DescriptionDAO>();
            foreach (Description description in videoContent.Descriptions)
                list.Add(description.ToDAO());
            var list1 = videoContent.References.Select(reference => reference.ToDAO()).ToList();
            var list2 = videoContent.Comments.Select(comment => comment.ToDAO()).ToList();
            var list3 = new List<MaterialDAO>();
            foreach (Material material in videoContent.Materials)
                list3.Add(material.ToDAO());
            var videoDAO = new VideoContentDAO()
                               {
                                   Id = videoContent.Id,
                                   Link = videoContent.Link,
                                   Text = videoContent.Text,
                                   Descriptions = list,
                                   References = list1,
                                   Comments = list2,
                                   Materials = list3
                               };
            return videoDAO;
        }
        public static Practice ToPracticeEntity(this PracticeDAO practiceDAO)
        {
            if (practiceDAO ==null)
            {
                return new Practice();
            }
            var practice = new Practice();
            practice.Id = practiceDAO.Id;
            foreach (var answerDAO in practiceDAO.Answers)
                practice.Answers.Add(answerDAO.ToAnswerEntity());
            practice.Question = practiceDAO.Question;
            return practice;
        }
        public static PracticeDAO ToPracticeDAO(this Practice practice)
        {
            if (practice ==null)
            {
                return new PracticeDAO();
            }
            var practiceDAO = new PracticeDAO();
            practiceDAO.Id = practice.Id;
            foreach (var answer in practice.Answers)
                practiceDAO.Answers.Add(answer.ToAnswerDAO());
            practiceDAO.Question = practice.Question;
            return practiceDAO;
        }
        
        public static AnswerDAO ToAnswerDAO(this Answer answer)
        {
            if (answer==null)
            {
                return new AnswerDAO();
            }
            var answerDAO = new AnswerDAO()
                                {
                                    Id = answer.Id,
                                    IsCorrect = answer.IsCorrect,
                                    Text = answer.Text
                                };
            return answerDAO;

        }
        public static Answer ToAnswerEntity(this AnswerDAO answer)
        {
            if (answer == null)
            {
                return new Answer();
            }
            var answerEntity = new Answer()
            {
                Id = answer.Id,
                IsCorrect = answer.IsCorrect,
                Text = answer.Text
            };
            return answerEntity;
        }

        public static Learner ToLearnerEntity(this LearnerDAO learnerDAO)
        {
            if (learnerDAO==null)
            {
                return new Learner();
            }
            Learner learner = new Learner();
            foreach (var attemptDao in learnerDAO.Attempts)
            learner.Attempts.Add(attemptDao.ToEntity());
            learner.EmailAddress = learnerDAO.EmailAddress;
            learner.Id = learnerDAO.Id;
            learner.Name = learnerDAO.Name;
            learner.Password = learnerDAO.Password;
            learner.Role = learnerDAO.Role;
            return learner;
        }
        public static LearnerDAO ToLearnerDAO(this Learner learnerDAO)
        {
            if (learnerDAO == null)
            {
                return new LearnerDAO();
            }
            LearnerDAO learner = new LearnerDAO();
            foreach (var attemptDao in learnerDAO.Attempts)
            learner.Attempts.Add(attemptDao.ToDAO());
            learner.EmailAddress = learnerDAO.EmailAddress;
            learner.Id = learnerDAO.Id;
            learner.Name = learnerDAO.Name;
            learner.Password = learnerDAO.Password;
            learner.Role = learnerDAO.Role;
            return learner;
        }

        public static Attempt ToEntity(this AttemptDAO attempt)
        {
            if (attempt==null)
            {
                return new Attempt();
            }
            var attemptEntity = new Attempt
                                    {
                                        Id = attempt.Id,
                                        IsCorrect = attempt.IsCorrect,
                                        Practice = attempt.Practice.ToPracticeEntity()
                                    };
            return attemptEntity;
        }
        public static AttemptDAO ToDAO(this Attempt attempt)
        {
            if (attempt == null)
            {
                return new AttemptDAO();
            }
            var attemptDAO = new AttemptDAO
            {
                Id = attempt.Id,
                IsCorrect = attempt.IsCorrect,
                Practice = attempt.Practice.ToPracticeDAO()
            };
            return attemptDAO;
        }
        public static MaterialDAO ToDAO(this Material material)
        {
            if (material==null)
            {
                return new MaterialDAO();
            }
            MaterialDAO materialDAO = new MaterialDAO()
                                          {
                                              Id = material.Id,
                                              Link = material.Link
                                          };
            return materialDAO;
        }
        public static Material ToEntity(this MaterialDAO material)
        {
            if (material == null)
            {
                return new Material();
            }
            Material materialEntity = new Material()
            {
                Id = material.Id,
                Link = material.Link
            };
            return materialEntity;
        }
        public static DescriptionDAO ToDAO(this Description description)
        {
           if(description==null)
           {return new DescriptionDAO();}
            DescriptionDAO descriptionDAO = new DescriptionDAO() {Id = description.Id, Text = description.Text};
            return descriptionDAO;
        }
        public static Description ToEntity(this DescriptionDAO description)
        {
            if (description == null)
            { return new Description(); }
            Description descriptionEntity = new Description() { Id = description.Id, Text = description.Text };
            return descriptionEntity;
        }
        public static Reference ToEntity(this ReferenceDAO reference)
        {
            if (reference ==null)
                return new Reference();
            Reference referenceEntity = new Reference() {Id = reference.Id, Link = reference.Link};
            return referenceEntity;
        }

        public static ReferenceDAO ToDAO(this Reference reference)
        {
            if (reference == null)
                return new ReferenceDAO();
            ReferenceDAO referenceDAO= new ReferenceDAO() { Id = reference.Id, Link = reference.Link };
            return referenceDAO;
        }

        public static CommentDAO ToDAO(this Comment comment)
        {
            if (comment==null)
            {
                return new CommentDAO();
            }
            CommentDAO commentDAO = new CommentDAO()
                                        {
                                            Date = comment.Date,
                                            Id = comment.Id,
                                            Learner = comment.Learner.ToLearnerDAO(),
                                            Text = comment.Text
                                        };
            return commentDAO;
        }
        public static Comment ToEntity(this CommentDAO comment)
        {
            if (comment == null)
            {
                return new Comment();
            }
            Comment commentEntity = new Comment()
            {
                Date = comment.Date,
                Id = comment.Id,
                Learner = comment.Learner.ToLearnerEntity(),
                Text = comment.Text
            };
            return commentEntity;
        }
    }
}

